package com.example.audiostreaming.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.audiostreaming.Bean.UserBean;
import com.example.audiostreaming.R;
import com.example.audiostreaming.base.BaseActivity;
import com.example.audiostreaming.utils.UtilUser;
import com.example.audiostreaming.views.InputView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ChangePasswordActivity extends BaseActivity {
    private static final String TAG = "ChangePasswordActivity";

    private InputView mOldPassword;
    private InputView mPassword;
    private InputView mPasswordConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initView();
    }

    private void initView() {
        initNavBar(true,"修改密码");
        mOldPassword = findViewById(R.id.input_old_password);
        mPassword = findViewById(R.id.input_password);
        mPasswordConfirm = findViewById(R.id.input_password_confirm);
    }

    public void onChangePasswordClick(View view) {
        String oldPassword = mOldPassword.getInputStr();
        String password = mPassword.getInputStr();
        String passwordConfirm = mPasswordConfirm.getInputStr();
        boolean result = UtilUser.changePassword(this, oldPassword, password, passwordConfirm);
        if (!result) {
            return;
        }

        changePassword(UserBean.getUserBean().getTelephone(),password);

        UtilUser.logicOut(this);
    }



    private void changePassword(String telephone, String password) {
        String mUrl = "http://192.168.0.107:8080/AndioStreamingService_war_exploded/";
        OkHttpClient okHttpClient = new OkHttpClient();

        UserBean userBean = UserBean.getUserBean();
        userBean.setTelephone(telephone);
//        userBean.setPassword(EncryptUtils.encryptMD5ToString(password));
        userBean.setPassword(password);

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        String s = gson.toJson(userBean);

        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=GBK"), s);
        Request.Builder builder = new Request.Builder();
        Request request = builder.url(mUrl + "changePassword").post(requestBody).build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure ==> "+e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Log.d(TAG, "onResponse: "+string);
            }
        });
    }
}
